const app = getApp()

const regeneratorRuntime = utils.regeneratorRuntime
import {
  userModel,commonModel
} from '../../apis/index.js'
import utils from '../../utils/index.js';

Page({
  data: {
   
  },

  async onLoad(options) {
    let userInfo =await userModel.getUserInfo()
    this.setData({
      userInfo:userInfo
    })
  },
  onReady: function () {

  },
  onShow: function () {

  },
  chooseImage(){
    let _this= this
    utils.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album','camera'],
      success: (result)=>{
        let filePath = result.tempFilePaths[0]
        commonModel.uploadFile({filePath}).then(res=>{
          _this.setData({
            [`userInfo.avatar`]:res
          })
        })
      }
    })
  },
  saveUserInfo(){
    let {avatar,name} = this.data.userInfo
    if(name.trim()==''){
      utils.showToast('昵称不能为空','none')
      return false
    }
    userModel.saveUserInfo({
      avatar:avatar,
      name:name
    }).then(res=>{
      utils.showToast('保存信息成功','none')
      setTimeout(()=>{
        utils.navigateBack({
          delta:1
        })
      },1500)
    })
  },
  onChange(e){
    let value = e.detail.value
    if(value.length>10){
      utils.showToast('昵称长度在10个字符以内','none')
      this.setData({
        [`userInfo.name`]:this.data.userInfo.name
      })
    }else{
      this.setData({
        [`userInfo.name`]:value
      })
    }
  }
})